package cn.bdqn.service.order;

import cn.bdqn.dao.order.UserOrderMapper;
import cn.bdqn.pojo.order.UserOrder;
import cn.bdqn.pojo.reportForms.OrderRegisterReportForms;
import cn.bdqn.utils.Json;
import com.baomidou.mybatisplus.extension.conditions.update.UpdateChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Objects;
import java.util.Random;
import java.util.UUID;
@Slf4j
@Service
public class UserOrderService extends ServiceImpl<UserOrderMapper, UserOrder> {
    @Autowired
    private UserOrderMapper userOrderMapper;

    @Transactional(rollbackFor = Exception.class)
    public Json getAllRoder() {
        for (int i = 0; i < 200; i++) {
            UserOrder userOrder = new UserOrder();
            userOrder.setOrderNumber(String.valueOf(UUID.randomUUID()));
            userOrder.setUserId((int)(Math.random()*100+1));
            userOrder.setOrderStateId(new Random().nextInt( 7 ) + 1);
            userOrder.setOrderType(new Random().nextInt(2)+1);
            userOrder.setOrderAmount((double) (Math.random()*100+1));
            save(userOrder);
            log.info("-----------执行成功-------------");
        }
        return Json.ok();
    }

    /**
     * 修改订单状态
     * @param userId  用户编号
     * @param ordeStateId 订单状态码
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public Json changeOrderState(String userId, Integer ordeStateId) {
        Boolean flag = userOrderMapper.changeOrderState(userId,ordeStateId);
        if (flag == false){
            return Json.fall("修改失败");
        }
        return Json.ok();
    }

    /**
     * 获取今天交易成功的订单量和金额
     * @return
     */
    public Json getTodayRoder() {
        OrderRegisterReportForms todayRoder = userOrderMapper.getTodayRoder();
        if (Objects.isNull(todayRoder)){
            return Json.fall("查询失败");
        }
        return Json.ok(todayRoder);
    }

    /**
     * 获取昨天交易成功的订量和
     * @return
     */
    public Json getYesterDay() {
       OrderRegisterReportForms orderRegisterReportForms =
               userOrderMapper.getYesterDay();
       if (Objects.isNull(orderRegisterReportForms)){
           return Json.fall("查询失败");
       }
       return Json.ok(orderRegisterReportForms);
    }

    /**
     * 获取近七天交易成功的订单量
     * @return
     */
    public Json getSevenDay() {
      OrderRegisterReportForms orderRegisterReportForms =
              userOrderMapper.getSevenDay();
      if (Objects.isNull(orderRegisterReportForms)){
          return Json.fall("查询失败");
      }
      return Json.ok(orderRegisterReportForms);
    }

    /**
     * 批量 删除订单
     * @param orderAll
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteAllOrder(Integer[] orderAll) {
        for (int i = 0; i < orderAll.length; i++) {
            int i1 = userOrderMapper.deleteById(orderAll[i]);
            if (i1 <= 0){
                throw new RuntimeException("执行失败");
            }
        }
        return true;
    }
}
